package thread;

/**
 * 互斥锁
 * 当使用多个synchronized锁定多个代码片段，并且指定的同步监视器对象时同一个时，那么
 * 这些代码片段之间就是胡吃的，多个线程不能同时执行他们。
 */
public class SyncDemo4 {
    public static void main(String[] args) {
        Boo b1 = new Boo();

        Thread t1 = new Thread("iu") {
            public void run() {
                b1.aoo();
            }
        };
        Thread t2 = new Thread("oj") {
            public void run() {
                b1.boo();
            }
        };
        t1.start();
        t2.start();
    }
}

    class Boo {
        public synchronized static void boo() {
            try {
                Thread t=Thread.currentThread();
                System.out.println(t.getName()+"正在");
                Thread.sleep(2000);
                System.out.println(t.getName()+"做着");
                Thread.sleep(2000);
                System.out.println(t.getName()+"完成");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        public void aoo() {
            synchronized (this) {
                try {
                    Thread t = Thread.currentThread();
                    System.out.println(t.getName()+"正在");
                    Thread.sleep(2000);
                    System.out.println(t.getName()+"做着");
                    Thread.sleep(2000);
                    System.out.println(t.getName()+"完成");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
}



